//admin device
$(function(){
	admin_device.run();
});

var admin_device = {
		run: function(){
			this._init_();
			this._start_();
			this._init_events();			
		},
		
		_init_: function(){
			this.contextPath = $("#contextPath").val() || "";
		},
		
		_start_: function(){			
		},
		
		_init_events: function(){
			$("#new_device_div").admindialog({
				offsetLeft: -20,
				offsetTop: -100,
				gatherData: true,
				submit: function(data){
					var url = admin_device.contextPath + "/admin/device/add";
					$.post(url, data, function(result){
						alert(result);
						location.reload(location.href);
					});
				}
			});
			
			$("#update_device_div").admindialog({
				gatherData: true,
				submit: function(data){
					var url = admin_device.contextPath + "/admin/device/update";
					$.post(url, data, function(result){
						alert(result);
						location.reload(location.href);
					});					
				}
			});
			
			$("#add_device_btn").click(function(){				
				$("#new_device_div").dialog('open');
			});
			
			$("#apply_device_btn").click(function(){
				var url = admin_device.contextPath + "/admin/device/clean";
				$.get(url, function(result){					
					alert(result);
					location.reload(location.href);
				});
			});
			
			$("#device_table  tr > td > a.update").click(function(){
				var deviceId = $(this).attr("deviceId");
				var url = admin_device.contextPath + "/admin/device/get/" + deviceId;
				$.get(url, function(result){
					$("#update_content_div").html(result);		
					$("#update_device_div").dialog('open');
				});
			});
			$("#device_table  tr > td > a.delete").click(function(){
				var checked = window.confirm("Are you sure ?");
				if(checked){
					var deviceId = $(this).attr("deviceId");
					var url = admin_device.contextPath + "/admin/device/delete/" + deviceId;
					$.get(url, function(result){
						alert(result);
						location.reload(location.href);
					});
				}				
			});
		}
};